package com.ptszyxx.popose.module.main.dynamic.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.event.CommentSuccessEvent;
import com.ptszyxx.popose.common.event.DynamicDeleteEvent;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.dynamic.DynamicResult;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class DynamicTabVM extends BaseViewModel<CommonRepository> {
    public int tab;//1.全部；2.关注
    public int currentPosition;
    public DynamicResult currentItem;

    public DynamicTabVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onLikeEvent = new SingleLiveEvent<>();
    }

    public Disposable disposable, disposableDelete;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(CommentSuccessEvent.class)
                .subscribe(new Consumer<CommentSuccessEvent>() {
                    @Override
                    public void accept(CommentSuccessEvent event){
                        if (event != null){
                            requestList(true);
                        }
                    }
                });
        disposableDelete = RxBus.getDefault().toObservable(DynamicDeleteEvent.class)
                .subscribe(new Consumer<DynamicDeleteEvent>() {
                    @Override
                    public void accept(DynamicDeleteEvent event){
                        if (event != null){
                            requestList(true);
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
        RxSubscriptions.remove(disposableDelete);
    }


    //=========================================接口：start===========================================
    public void requestList(boolean isRefresh) {
        if (isRefresh){
            page = 1;
        }else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", String.valueOf(page));

        Observable observable = model.dynamicPage(map);
        if (2 == tab){
            map.put("pageIndex", String.valueOf(page));
            observable = model.dynamicFollowPage(map);
        }

        HttpUtils.getInstance()
                .list(observable, this,
                        new OnSuccessListResult<DynamicResult>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<DynamicResult> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 点赞
     */
    public void requestDynamicLike(DynamicResult item) {
        String type;
        if (YStringUtil.eq(1, item.getDzstate())){
            type = "2";
        }else{
            type = "1";
        }
        Map<String, String> map = new HashMap<>();
        map.put("dongtaiId", item.getId() + "");
        map.put("type", type);
        HttpUtils.getInstance()
                .data(model.dynamicLike(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(response.getMsg());
                                uc.onLikeEvent.call();
                            }
                        });
    }

    /**
     * 动态详情
     */
    public void jumpDynamicDetail(DynamicResult entity){
//        YActivityUtil.getInstance().jumpDynamicDetail(DynamicTabVM.this, entity.getId()+"");
    }

    /**
     * 头像：个人详情
     */
    public void onAvatarClick(DynamicResult result){
        YActivityUtil.getInstance().jumpUserDetail(DynamicTabVM.this, result.getUserid()+"");
    }

    /**
     * 点赞
     */
    public void onLikeClick(DynamicResult item, int position){
        currentPosition = position;
        currentItem = item;
        requestDynamicLike(item);
    }

}
